package common.test;

import java.util.HashSet;
import java.util.Set;

/**
 * 3. 无重复字符的最长子串
 */
public class _3_LengthOfLongestSubstring {

    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        int ans = 0;
        Set<Character> set = new HashSet<>();
        for (int i = 0, j = 0; j < s.length(); ) {
            char c = s.charAt(j);
            if (set.contains(c)) {
                set.remove(s.charAt(i));
                i++;
            } else {
                set.add(c);
                j++;
            }
            ans = Math.max(ans, set.size());
        }
        return ans;
    }
}
